package com.bdqn.qnnews.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdqn.qnnews.common.base.R;
import com.bdqn.qnnews.wemedia.api.dto.WmUserDto;
import com.bdqn.qnnews.wemedia.convert.WmUserConvert;
import com.bdqn.qnnews.wemedia.entity.WmUser;
import com.bdqn.qnnews.wemedia.service.WmUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author xlz
 * @since 2023-12-26
 * @description 自媒体用户信息表
 */
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/api/v1")
@RestController
@Tag(name = "自媒体用户信息表控制器类",
        description = "WmUserController 自媒体用户信息表 后端数据接口")
public class WmUserController {

    private final WmUserService wmUserService;

    @Operation(summary = "根据姓名查询自媒体用户信息")
    @GetMapping("/user")
    public R<WmUserDto> findByName(@RequestParam("name") String name) {
        LambdaQueryWrapper<WmUser> wrapper = Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name);
        WmUser user = wmUserService.getOne(wrapper);
        if (user != null) {
            final WmUserDto wmUserDto = WmUserConvert.INSTANCE.entityToDto(user);
            wmUserDto.setPassword("");
            return R.ok(wmUserDto);
        }
        return R.ok();
    }

    /**
     * 当前保存用户信息后，需要返回用户信息给调用方法
     * @param wmUserDto
     * @return
     */
    @Operation(summary = "保存自媒体用户信息", description = "通过app端远程调用访问")
    @PostMapping("/user")
    public R<WmUserDto> save(@RequestBody WmUserDto wmUserDto) {
        final WmUser wmUser = WmUserConvert.INSTANCE.dtoToEntity(wmUserDto);
        wmUserService.save(wmUser);

        // 将保存成功的用户的id赋值给dto
        wmUserDto.setId(wmUser.getId());

        return R.ok(wmUserDto);
    }
}
